import { Router } from 'director/build/director';
import Util from './common-component/util/util';
import API from './api/Api.js';
import Index from './index.js';
import './asset/reset.scss';

// 引入垫片兼容IE
import "babel-polyfill";

/*按需加载require方式，和组件里面的module.exports对应*/
/*首页*/
const HomePageCb = function() {
    require.ensure([], (require) => {
        let HomePage = require('./pages/home-page/home-page.js');
        HomePage.default();
    },'HomePage')
};
/*内容模块－新闻*/
const contentNews = function() {
    require.ensure([], (require) => {
        let contentNews = require('./pages/content/news/news.js');
        contentNews.default();
    },'contentNews')
};
/*内容模块－新闻新增*/
const contentNewsAdd = function() {
    require.ensure([], (require) => {
        let contentNewsAdd = require('./pages/content/news/add/news.js');
        contentNewsAdd.default();
    },'contentNewsAdd')
};
/*内容模块－新闻编辑*/
const contentNewsUpdate = function() {
    require.ensure([], (require) => {
        let contentNewsUpdate = require('./pages/content/news/update/news.js');
        contentNewsUpdate.default();
    },'contentNewsUpdate')
};
/*内容模块－案例*/
const contentCase = function() {
    require.ensure([], (require) => {
        let contentCase = require('./pages/content/case/case.js');
        contentCase.default();
    },'contentCase')
};
/*内容模块－案例新增*/
const contentCaseAdd = function() {
    require.ensure([], (require) => {
        let contentCaseAdd = require('./pages/content/case/add/case.js');
        contentCaseAdd.default();
    },'contentCaseAdd')
};
/*内容模块－案例编辑*/
const contentCaseUpdate = function() {
    require.ensure([], (require) => {
        let contentNewsUpdate = require('./pages/content/case/update/case.js');
        contentNewsUpdate.default();
    },'contentNewsUpdate')
};
/*内容模块－产品*/
const contentProduct = function() {
    require.ensure([], (require) => {
        let contentProduct = require('./pages/content/product/product.js');
        contentProduct.default();
    },'contentProduct')
};
/*内容模块－产品新增*/
const contentProductAdd = function() {
    require.ensure([], (require) => {
        let contentProductAdd = require('./pages/content/product/add/product.js');
        contentProductAdd.default();
    },'contentProductAdd')
};
/*内容模块－产品编辑*/
const contentProductUpdate = function() {
    require.ensure([], (require) => {
        let contentProductUpdate = require('./pages/content/product/update/product.js');
        contentProductUpdate.default();
    },'contentProductUpdate')
};
/*招聘模块*/
const recruit = function() {
    require.ensure([], (require) => {
        let recruit = require('./pages/recruit/recruit.js');
        recruit.default();
    },'recruit')
};
/*招聘模块－新增*/
const recruitAdd = function() {
    require.ensure([], (require) => {
        let recruitAdd = require('./pages/recruit/add/recruit.js');
        recruitAdd.default();
    },'recruitAdd')
};
/*留言模块－列表*/
const commentList = function() {
    require.ensure([], (require) => {
        let commentList = require('./pages/comment/comment-list/comment-list.js');
        commentList.default();
    },'commentList')
};
/*留言模块－ 设置*/
const commentSetUp = function() {
    require.ensure([], (require) => {
        let commentSetUp = require('./pages/comment/comment-SetUp/comment-SetUp.js');
        commentSetUp.default();
    },'commentSetUp')
};
/*留言模块－详情*/
const commentDetail = function() {
    require.ensure([], (require) => {
        let commentDetail = require('./pages/comment/comment-detail/comment-detail.js');
        commentDetail.default();
    },'commentDetail')
};
/* 下载模块－列表*/
const downloadList = function() {
    require.ensure([], (require) => {
        let downloadList = require('./pages/download/download.js');
        downloadList.default();
    },'downloadList')
};
/* 下载模块－新增*/
const downloadAdd = function() {
    require.ensure([], (require) => {
        let downloadAdd = require('./pages/download/add/download.js');
        downloadAdd.default();
    },'downloadAdd')
};
/* 下载模块－编辑*/
const downloadUpdate = function() {
    require.ensure([], (require) => {
        let downloadUpdate = require('./pages/download/update/download.js');
        downloadUpdate.default();
    },'downloadUpdate')
};
/* 设置－新闻分类*/
const settingNews = function() {
    require.ensure([], (require) => {
        let settingNews = require('./pages/Setting/news/news.js');
        settingNews.default();
    }, 'settingNews')
}
/* 设置－案例分类*/
const settingCase = function() {

    require.ensure([], (require) => {
        let settingCase = require('./pages/Setting/case/case.js');
        settingCase.default();
    },'settingCase')
}
/* 设置－产品分类*/
const settingProduct = function() {
    require.ensure([], (require) => {
        let settingProduct = require('./pages/Setting/product/product.js');
        settingProduct.default();
    },'settingProduct')
};
const routes = {
    '/': () => {
        let userId = Util.getCookie('userId');
        Util.linkTo('/home-page');
    },
    '/home-page': HomePageCb,
    '/news': contentNews,
    '/news-add': contentNewsAdd,
    '/news-update': contentNewsUpdate,
    '/case': contentCase,
    '/case-add': contentCaseAdd,
    '/case-update': contentCaseUpdate,
    '/product': contentProduct,
    '/product-add': contentProductAdd,
    '/product-update': contentProductUpdate,
    '/recruit': recruit,
    '/recruit-add': recruitAdd,
    '/comment-list': commentList,
    '/comment-setUp': commentSetUp,
    '/comment-detail': commentDetail,
    '/download-list': downloadList,
    '/download-add': downloadAdd,
    '/download-update': downloadUpdate,
    '/setting-news': settingNews,
    '/setting-case': settingCase,
    '/setting-product': settingProduct,
};

const router = new Router(routes).configure({
    notfound: () => {
        alert('错误链接！');
    },
    before: () => {
        let token = Util.getCookie('token');
        if(!token && location.hash.indexOf('/login') < 0){
           window.location.href = '/login.html'
          return false;
        }
    }
});
router.init();

//初始化默认路由
if(!Util.getRouter()){
    Util.linkTo('/');
}


/**
 * Created with JetBrains WebStorm.
 * User: liyong.wang
 * Date: 17/3/8
 * Time: 上午9:57
 * Desc: 用户未登录统一拦截模块.
 */
$(document).ajaxComplete(function(e,xhr,st){
    var status = xhr.status;
    if(status == 401 ){//用户未登录  则删除token跳转登录
        Util.deleteCookie("token",document.domain);
        window.location.href = '/login.html'
    }
});


